
package cc.ServiceImpl;

import cc.Dao.ContentDao;
import cc.Entity.Category;
import cc.Entity.ContentInfo;
import cc.Service.ContentService;
import cc.Utils.UUIDUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: ${张智杰}
 * @Date: 2018/10/7 19:05
 * @Description:
 */

@Service
public class ContentServiceImpl implements ContentService {


    @Autowired
    private ContentDao contentDao;
    //发布文章
    @Override
    public int addContent(ContentInfo contentInfo){


        /*if (contentInfo.getInfoId()==null || contentInfo.getInfoId().equals("")){
           return 0;
        }else{*/
            contentInfo.setInfoId(UUIDUtil.getUUID32());
            contentInfo.setInfoReltime(String.valueOf(System.currentTimeMillis()));
            return contentDao.addContent(contentInfo);


    }
    //后台搜索框搜索，作者，标题，id
    @Override
    public List<ContentInfo> getContent(ContentInfo contentInfo){
        if((contentInfo.getInfoHeadline()==null || contentInfo.getInfoHeadline().equals(""))
                && (contentInfo.getInfoAuthor()==null || contentInfo.getInfoAuthor().equals(""))
                && (contentInfo.getInfoId()==null || contentInfo.getInfoId().equals(""))){
                return null;
        }
        return contentDao.getContent(contentInfo);
    }
    //删除文章
    @Override
   public int delContent(String infoId){
       if (infoId==null || infoId.equals("")){
           return 0;
       }else {
          return contentDao.delContent(infoId);
       }
    }
    //查看单条新闻内容
    @Override
    public ContentInfo getConContent(String infoId, HttpServletRequest request){
        if(infoId==null || infoId.equals("")){
            return null;
        }else {

            /**
             * 记录访问量
             */
            if(request.getSession().getAttribute(infoId)==null){
                request.getSession().setAttribute(infoId,1);
            }else {
                request.getSession().setAttribute(infoId,((Integer)(request.getSession().getAttribute(infoId)))+1);
            }
            //更新总访问量
            this.updatePvById(infoId);

            return contentDao.getConContent(infoId);
        }
    }
    //通过type来查找，前端震情信息，新闻事件，地震科普
    @Override
    public List<ContentInfo> getContentByType(String infoType){
        if(infoType==null || infoType.equals("")){
            return null;
        }else {
            if(infoType.equals("1")){
                infoType = "震情信息";
            }else if(infoType.equals("2")){
                infoType = "新闻事件";
            }else{
                infoType = "科普知识";
            }
            return contentDao.getContentByType(infoType);
        }
    }

    //在后台查询
    @Override
    public List getBackContent(){

            return contentDao.getBackContent();

    }
    public int updatePvById(String id){
       return contentDao.updatePvById(id);
    }
    //编辑
    @Override
    public int updateContentById(@Param("infoId") String infoId, @Param("infoContent") String infoContent){
        if(infoId == null || infoId ==""){
            return 0;
        }else{

            return contentDao.updateContentById(infoId,infoContent);
        }
    }
    //点赞数
    @Override
    public int updateGreatNum(@Param("infoId")String infoId){
        if(infoId == null || infoId == ""){
            return 0;
        }else{
            return contentDao.updateGreatNum(infoId);
        }
    }
    //通过类型来查category,后端登录后默认选择所有，当选择其他类别时，才调用这个借口
    public List<Category> getBackContentByCategory(String info_category){
        if (info_category == null || info_category.equals("")){
            return null;
        }else{
           return contentDao.getBackContentByCategory(info_category);

        }
    }
}

